package com.gkj.controller;

import com.gkj.blog.business.enums.ResponseStatus;
import com.gkj.blog.framework.object.ResponseVO;
import com.gkj.blog.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/api/pass")
public class PassController {
    Logger logger= LoggerFactory.getLogger(PassController.class);
    @PostMapping("/login")
    @ResponseBody
    public ResponseVO login(String username,String password){
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return ResultUtil.success(ResponseStatus.SUCCESS);
        }catch (Exception e){
            token.clear();
            logger.error("登录失败，用户名:{}=={}",username,e.getMessage());
            return ResultUtil.error(e.getMessage());
        }
    }

    @RequestMapping("logout")
    public ModelAndView logout(){
        return ResultUtil.redirect("/login.html");
    }
}
